<?php

class View
{
	public $renderer;
	public $layoutDir = 'layouts';
	public $layout = 'default.tpl';
	public $template;
	
	public function __construct($sLayout = 'default.tpl') 
	{
		$this->renderer = new Smarty();
		$this->renderer->template_dir = BASE_DIR.'/templates/';
		$this->renderer->compile_dir = BASE_DIR.'/tmp/templates_c/';
		
		$this->layout = $sLayout;
		
		// присваиваем глобальные переменные
		$this->renderer->assign("current_user", getCurrentUser());
		$this->renderer->assign("sLayoutPath", $this->renderer->template_dir.$this->layoutDir.'/');
		// extravert
		if (isset($GLOBALS['global_datehelper']) && $GLOBALS['global_datehelper'])
		{
		    $this->renderer->assign("datehelper", $GLOBALS['global_datehelper']);
		}
		$this->renderer->assign("host_name", 'http://'.HOST_NAME);
		$this->renderer->assign("host_name_short", HOST_NAME_SHORT);
		$this->renderer->assign("site_name", SITE_NAME);
		$this->renderer->assign("spart", getSpart());
		$this->renderer->assign("is_crawler", isCrawler());
		$this->renderer->assign("selfurl", $GLOBALS['selfurl']);
		$this->renderer->assign("cururl", $GLOBALS['cururl']);
		
		$ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
		if( is_int(strpos($ua,'ipad')) || is_int(strpos($ua,'iphone')) ) $this->renderer->assign("iphone", 1);
		if( is_int(strpos($ua,'midp')) ) $this->renderer->assign("midp", 1);
		if( is_int(strpos($ua,'webkit')) ) $this->renderer->assign("webkit", 1);
		
		// extravert
		if (isset($GLOBALS['siterazdel']) && $GLOBALS['siterazdel'])
		{
		    $this->renderer->assign("siterazdel", $GLOBALS['siterazdel']);
		}
		else
		{
		    $GLOBALS['siterazdel'] = 'Хороший вопрос';
		}
		switch($GLOBALS['siterazdel'])
		{
			case 'diets':
				$this->renderer->assign("current_mmenu", 2);
				break;
			case 'articles':
				$this->renderer->assign("current_mmenu", 3);
				break;
			case 'blogs':
				$this->renderer->assign("current_mmenu", 4);
				break;
			case 'groups':
				$this->renderer->assign("current_mmenu", 5);
				break;
			case 'club':
				$this->renderer->assign("current_mmenu", 6);
				break;
			case 'photo':
				$this->renderer->assign("current_mmenu", 7);
				break;
			case 'qa':
				$this->renderer->assign("current_mmenu", 8);
				break;
			case 'users':
				$this->renderer->assign("current_mmenu", 9);
				break;
			default:
				$this->renderer->assign("current_mmenu", 1);
		}
	}
	
	public function render($template) 
	{
	    $this->renderer->assign('sTplName', $this->renderer->template_dir.$template);
		return $this->renderer->fetch($this->layoutDir.'/'.$this->layout);
		//return prepareOutput($this->renderer->fetch($this->template)); //for subdomains etc.
	}
	
	public function add($key, $value = null)
	{
		$this->renderer->assign($key, $value);
	}
}